-
Notifications
You must be signed in to change notification settings - Fork 29.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
stream: convert string to Buffer when calling unshift(<string>)
#27194
Conversation
Can you add a test for this? |
@Trott added multiple cases, if something is missing or needs to be changed let me know. |
Thanks! Only other thing to add that I can see is the R: @nodejs/streams |
I think this is semver-minor as it's adding a new parameter. CI: https://ci.nodejs.org/job/node-test-pull-request/22398/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you also add a test for how this interacts with .setEncoding()
?
I'll submit one in a few. Forgot about that scenario, thanks! |
@addaleax When doing that test, I remember that when I don't think this PR should change that, I'm not even sure if it should be saved as a Buffer in that case, even though it's called So I can test that the behaviour is unchanged, meaning that they're saved as string, and that Is that ok? |
There is one change left, but would like some feedback. When But in order to keep the same behaviour, I have to convert the pushed string, to the encoding defined by
Because the string must be saved in BufferList in the same encoding as Thoughts? Maybe someone has another alternative. |
Is there something else that should be added/changed? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’ve left some notes related to the tests. I think we should remove the usage of private API as much as possible - the majority of this feature should be testable without it.
assert.strictEqual( | ||
readable._readableState.buffer.head.data, | ||
chunk | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test can be rewritten without using internals, it should consume the stream and check that the chunks comes out in order.
assert.strictEqual( | ||
readable._readableState.buffer.head.data.toString('utf8'), | ||
string | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be tested by consuming the stream and verifying that the data comes out in order.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
This comment has been minimized.
This comment has been minimized.
`readable.unshift` can take a string as an argument, but that string wasn't being converted to a Buffer, which caused a <TypeError: Argument must be a buffer> in some cases. Also if a string was passed, that string was coerced to utf8 encoding. A second optional argument `encoding` was added to `unshift` to fix the encoding issue. Fixes: #27192 PR-URL: #27194 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Rich Trott <[email protected]>
Landed in df339bc |
`readable.unshift` can take a string as an argument, but that string wasn't being converted to a Buffer, which caused a <TypeError: Argument must be a buffer> in some cases. Also if a string was passed, that string was coerced to utf8 encoding. A second optional argument `encoding` was added to `unshift` to fix the encoding issue. Fixes: #27192 PR-URL: #27194 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Rich Trott <[email protected]>
Notable changes: * doc: * The JSON variant of the API documentation is no longer experimental (Rich Trott) #27842. * esm: * JSON module support is always enabled under `--experimental-modules`. The `--experimental-json-modules` flag has been removed (Myles Borins) #27752. * http,http2: * A new flag has been added for overriding the default HTTP server socket timeout (which is two minutes). Pass `--http-server-default-timeout=milliseconds` or `--http-server-default-timeout=0` to respectively change or disable the timeout. Starting with Node.js 13.0.0, the timeout will be disabled by default (Ali Ijaz Sheikh) #27704. * inspector: * Added an experimental `--heap-prof` flag to start the V8 heap profiler on startup and write the heap profile to disk before exit (Joyee Cheung) #27596. * stream: * The `readable.unshift()` method now correctly converts strings to buffers. Additionally, a new optional argument is accepted to specify the string's encoding, such as `'utf8'` or `'ascii'` (Marcos Casagrande) #27194. * v8: * The object returned by `v8.getHeapStatistics()` has two new properties: `number_of_native_contexts` and `number_of_detached_contexts` (Yuriy Vasiyarov) #27933. PR-URL: #28040
Notable changes: * doc: * The JSON variant of the API documentation is no longer experimental (Rich Trott) nodejs#27842. * esm: * JSON module support is always enabled under `--experimental-modules`. The `--experimental-json-modules` flag has been removed (Myles Borins) nodejs#27752. * http,http2: * A new flag has been added for overriding the default HTTP server socket timeout (which is two minutes). Pass `--http-server-default-timeout=milliseconds` or `--http-server-default-timeout=0` to respectively change or disable the timeout. Starting with Node.js 13.0.0, the timeout will be disabled by default (Ali Ijaz Sheikh) nodejs#27704. * inspector: * Added an experimental `--heap-prof` flag to start the V8 heap profiler on startup and write the heap profile to disk before exit (Joyee Cheung) nodejs#27596. * stream: * The `readable.unshift()` method now correctly converts strings to buffers. Additionally, a new optional argument is accepted to specify the string's encoding, such as `'utf8'` or `'ascii'` (Marcos Casagrande) nodejs#27194. * v8: * The object returned by `v8.getHeapStatistics()` has two new properties: `number_of_native_contexts` and `number_of_detached_contexts` (Yuriy Vasiyarov) nodejs#27933. PR-URL: nodejs#28040
Should this land on v10.x? //cc @mcollina |
Yes please! |
readable.unshift
can take a string as an argument, but thatstring wasn't being converted to a Buffer, which caused a
TypeError: Argument must be a buffer
in some cases.A second optional argument
encoding
was added tounshift
toprevent all strings being coerced to utf8.
Fixes: #27192
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesA documentation change will be needed, for the
encoding
argument, but wanted to submit the PR first to see if it will get approved. I will also make a few tests for this if everything is okay.